using UnityEngine;

namespace GraphEditor.Runtime
{
	/// <summary>
	/// GraphCanvas的持有者。
	/// 这个类并非具体逻辑处理类。它的主要作用是提供一个简单的Mono层编辑器容器，以便运行时Debug调试。
	/// </summary>
	public class GraphCanvasHolder : MonoBehaviour
	{
		/// <summary>
		/// 编辑器模式下的GraphCanvas实例
		/// </summary>
		public GraphCanvas graphCanvas;

#if UNITY_EDITOR
		/// <summary>
		/// 运行时构建一个新的GraphCanvas实例来进行操作
		/// </summary>
		private GraphCanvas _graphCanvasInstance;
		/// <summary>
		/// 运行时的逻辑类实例（运行时的话，Editor调试数据直接读取这个实例里的就行，至于节点编辑器的界面，则沿用graphCanvasInstance内的数据）
		/// </summary>
		private object _runtimeInstance;
#endif

		/// <summary>
		/// 绑定运行时实例
		/// </summary>
		/// <param name="runtimeInstance"></param>
		public void Bind(object runtimeInstance)
		{
#if UNITY_EDITOR
			_runtimeInstance = runtimeInstance;
			_graphCanvasInstance = Instantiate(graphCanvas);
#endif
		}

		/// <summary>
		/// 解绑运行时实例
		/// </summary>
		public void Unbind()
		{
#if UNITY_EDITOR
			Destroy(_graphCanvasInstance);
			_graphCanvasInstance = null;
			_runtimeInstance = null;
#endif
		}
	}
}